package com.example.cdpbookstore.service.impl;

import com.example.cdpbookstore.dao.BookDao;
import com.example.cdpbookstore.dao.OrderDao;
import com.example.cdpbookstore.dao.OrderDetailDao;
import com.example.cdpbookstore.dao.impl.BookDaoImpl;
import com.example.cdpbookstore.dao.impl.OrderDaoImpl;
import com.example.cdpbookstore.dao.impl.OrderDetailDaoImpl;
import com.example.cdpbookstore.pojo.entity.Book;
import com.example.cdpbookstore.pojo.entity.Order;
import com.example.cdpbookstore.pojo.entity.OrderDetail;
import com.example.cdpbookstore.service.OrderService;

import java.math.BigDecimal;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private final OrderDao orderDao = new OrderDaoImpl();
    private final OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
    private final BookDao bookDao = new BookDaoImpl();

    @Override
    public boolean saveOrderWithDetail(Order order) {
        // 检查库存量
        for (OrderDetail orderDetail : order.getOrderDetailList()) {
            Book book = bookDao.getById(orderDetail.getBookId());
            if (book.getStock() <= 0 || book.getStock() < orderDetail.getNum()) {
                return false;
            }
        }


        // 保存订单详情
        for (OrderDetail orderDetail : order.getOrderDetailList()) {
            Book book = bookDao.getById(orderDetail.getBookId());
            book.setStock(book.getStock() - orderDetail.getNum());
            if (!(orderDetailDao.save(orderDetail) && bookDao.update(book))) {
                return false;
            }
        }

        // 保存订单
        // 返回保存是否成功
        // TODO: 2024/5/25 数据库事务
        return orderDao.save(order);
    }

    @Override
    public List<Order> listByCustomerId(String customerId) {
        List<Order> orderList = orderDao.listByCustomerId(customerId);
        if (orderList == null) {
            return null;
        } else {
            // 为 orderList 列表里每个 item 添加 OrderDetailList
            for (Order order : orderList) {
                List<OrderDetail> orderDetailList = orderDetailDao.listByOrderId(order.getId());
                if (orderDetailList == null) {
                    // 理论上一个订单中必须有订单详情，如果没有（null），则说明程序有 bug
                    return null;
                } else {
                    // 用Stream 给订单添加上图书对象
                    orderDetailList.forEach(e -> {
                        Book book = bookDao.getById(e.getBookId());
                        e.setBook(book);
                    });
                }
                order.setOrderDetailList(orderDetailList);

                // 计算总金额
                BigDecimal totalAmount = orderDetailList.stream().map(e -> e.getUnitPrice().multiply(new BigDecimal(e.getNum()))).reduce(BigDecimal.ZERO, BigDecimal::add);
                order.setTotalAmount(totalAmount);
            }
            return orderList;
        }
    }

    @Override
    public boolean cancelOrder(String id) {
        return orderDao.removeById(id);
    }
}
